clear*
set maxvar 11000

global date _2025_10_28
cd "/Users/atri0032/Dropbox/World Bank/Temperature, poverty, and inequality/Estimations/"

******************************************************************************************************************************************************************
********************************** Figure S7: The effects of temperature on poverty and inequality across countries adjusted by real GDP using median temperature
******************************************************************************************************************************************************************

************* Mean temperature
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 

// country name
sort code
	merge m:1 code using "${date}/_data/country_name.dta"
	keep if _merge==3
	drop _merge
	
encode code, gen(id_country_num)

preserve
keep code id_country_num country_name
duplicates drop code id_country_num country_name, force
tempfile country_name
save `country_name'
restore
	
// Find countries with poverty around sample mean
su poor215, d

// Collapse data to find average poverty at country level
preserve
collapse (mean) poor215, by(code country_name id_country_num)
list code country_name id_country_num poor215 if inrange(poor215,7,10)
restore

// regression
reghdfe poor215 temp c.temp#b56.id_country_num rainfall_sum humid `controls', absorb(year) cluster(geo_code2_new) keepsingle 

parmest, saving("_tab_s18_poverty.dta", replace)
	
use "_tab_s18_poverty.dta", clear

// Keep interaction term estimates only
keep if strpos(parm, "#")

// Extract country code
gen id_country_num = real(regexs(1)) if regexm(parm,"([0-9]+)")

sort id_country_num
	merge 1:1 id_country_num using `country_name'
	keep if _merge==3
	drop _merge
	
gen sig= p <0.1
	
sort code
	merge 1:1 code using "${date}/_data/population_gdp_country.dta"
	drop if _merge==2
	drop _merge	
	
// Median temperature
preserve
use "${date}/_data/spid_for_analysis_v2.dta", clear
collapse (mean) temp_median, by(code)
tempfile temp_median
save `temp_median'
restore

sort code
	merge 1:1 code using `temp_median'
	drop if _merge==2
	drop _merge		
	
gen country_name_overlap = country_name

// Drop countries with extreme values
drop if inlist(code,"STP","AZE","BIH")
drop if country_name == ""

egen clock = mlabvpos(estimate temp_median), matrix(11 1 12 11 1 \\ 10 2 12 10 2 \\ 9 3 12 9 3 \\ 8 4 6 8 4 \\ 7 5 6 7 5)
	replace clock = 6 if inlist(country_name,"Mozambique","Ghana","Gambia","Ethiopia","Swaziland")	
	replace clock = 2 if inlist(country_name,"Uganda")	
	replace clock = 5 if inlist(country_name,"Botswana")	
	replace clock = 12 if inlist(country_name,"Chad")	
	replace clock = 3 if inlist(country_name,"Sudan")	

// temperature effect for the baseline country: 0.092 (Table S18)
replace estimate = estimate + 0.092
	
twoway (scatter  estimate temp_median [w=GDP] if sig==0, msymbol(O) mcolor(gs14)) ///
		(scatter  estimate temp_median if sig==0, mlabel(country_name) mlabcolor(maroon) ///
		mlabsize(tiny) msymbol(Oh) mlwidth(thin) msize(vsmall) mcolor(none) mlabvpos(clock)) ///
		(scatter  estimate temp_median [w=GDP] if sig>0, msymbol(O) mcolor(navy%60)) ///
		(scatter  estimate temp_median if sig>0, mlabel(country_name) mlabcolor(dkorange) ///
		mlabsize(tiny) msymbol(Oh) mlwidth(thin) msize(vsmall) mcolor(none) mlabvpos(clock)), ///		
		legend(off) graphregion(fcolor(white) lcolor(white) ilcolor(white) ifcolor(white)) ///
		plotregion(lcolor(black)  ilcolor(none) style(none) ) xlabel(, nogrid) bgcolor(white) ///
		yline(0.070, lwidth(thin) lcolor(black) lpattern(dash))   ylabel(, nogrid) ///
		xtitle("Median temperature") ytitle("Predicted effect of warmer temperature") scheme(white_tableau)
		
graph export "${date}/_figures/_fig_s7a.pdf", as(pdf) replace	

**************************************** Inequality

use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 

// country name
sort code
	merge m:1 code using "${date}/_data/country_name.dta"
	keep if _merge==3
	drop _merge
	
encode code, gen(id_country_num)

preserve
keep code id_country_num country_name
duplicates drop code id_country_num country_name, force
tempfile country_name
save `country_name'
restore
	
su gini, d

// Collapse data to find average poverty at country level
preserve
collapse (mean) gini, by(code country_name id_country_num)

list code country_name id_country_num gini if inrange(gini,35,35.5)
restore

// regression
reghdfe gini temp c.temp#b79.id_country_num rainfall_sum humid `controls', absorb(year) cluster(geo_code2_new) keepsingle 

parmest, saving("_tab_s18_inequality.dta", replace)
	
use "_tab_s18_inequality.dta", clear

// Keep interaction term estimates only
keep if strpos(parm, "#")

// Extract country code
gen id_country_num = real(regexs(1)) if regexm(parm,"([0-9]+)")

sort id_country_num
	merge 1:1 id_country_num using `country_name'
	keep if _merge==3
	drop _merge
	
gen sig= p <0.1
	
sort code
	merge 1:1 code using "${date}/_data/population_gdp_country.dta"
	drop if _merge==2
	drop _merge	
	
// Median temperature
preserve
use "${date}/_data/spid_for_analysis_v2.dta", clear
collapse (mean) temp_median, by(code)
tempfile temp_median
save `temp_median'
restore

sort code
	merge 1:1 code using `temp_median'
	drop if _merge==2
	drop _merge	
	
gen country_name_overlap = country_name

egen clock = mlabvpos(estimate temp_median), matrix(11 1 12 11 1 \\ 10 2 12 10 2 \\ 9 3 12 9 3 \\ 8 4 6 8 4 \\ 7 5 6 7 5)
	replace clock = 6 if inlist(country_name,"Mozambique","Ghana","Gambia","Ethiopia","Swaziland")	
	replace clock = 2 if inlist(country_name,"Uganda")	
	replace clock = 5 if inlist(country_name,"Botswana")	
	replace clock = 12 if inlist(country_name,"Chad")	
	replace clock = 3 if inlist(country_name,"Sudan")	
	
// temperature effect for the baseline country: 0.024 (Table S18)
replace estimate = estimate + 0.024
	
twoway (scatter  estimate temp_median [w=GDP] if sig==0, msymbol(O) mcolor(gs14)) ///
		(scatter  estimate temp_median if sig==0, mlabel(country_name) mlabcolor(maroon) ///
		mlabsize(tiny) msymbol(Oh) mlwidth(thin) msize(vsmall) mcolor(none) mlabvpos(clock)) ///
		(scatter  estimate temp_median [w=GDP] if sig>0, msymbol(O) mcolor(navy%60)) ///
		(scatter  estimate temp_median if sig>0, mlabel(country_name) mlabcolor(dkorange) ///
		mlabsize(tiny) msymbol(Oh) mlwidth(thin) msize(vsmall) mcolor(none) mlabvpos(clock)), ///		
		legend(off) graphregion(fcolor(white) lcolor(white) ilcolor(white) ifcolor(white)) ///
		plotregion(lcolor(black)  ilcolor(none) style(none) ) xlabel(, nogrid) bgcolor(white) ///
		yline(0.052, lwidth(thin) lcolor(black) lpattern(dash))   ylabel(, nogrid) ///
		xtitle("Median temperature") ytitle("Predicted effect of warmer temperature") scheme(white_tableau)
		
graph export "${date}/_figures/_fig_s7b.pdf", as(pdf) replace	
